import uitls.ArrayUtils;

public class No_001_Binary {

    public static void main(String[] args) {
        int[] array = ArrayUtils.randomArray(5, 1, 15);

        int res1 = search(array, 5);
        int res2 = search(array, 5);

        System.out.println(res1 == res2);
    }

    public static int search(int[] arr, int target) {
        if (arr == null || arr.length == 0) {
            return -1;
        }

        int left = 0, right = arr.length - 1, middle;
        while (left <= right) {
            middle = left + (right - left) >> 1;
            if (arr[middle] == target) {
                return middle;
            }
            if (arr[middle] < target) {
                left = middle + 1;
            }
            if (arr[middle] > target) {
                right = middle - 1;
            }
        }
        return -1;
    }

    public static int right(int[] arr, int target) {
        if (arr == null || arr.length == 0) {
            return -1;
        }
        for (int i = arr.length - 1; i >= 0; i--) {
            if (arr[i] == target) {
                return i;
            }
        }
        return -1;
    }

}
